package com.chencs.common.menu.controller;

import com.alibaba.fastjson.JSON;
import com.chencs.common.menu.service.MenuService;
import com.chencs.vo.common.menu.MenuModel;
import framework.model.ResponseOptUtil;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RestController;

/**
 * Created by fangzehua on 2017/6/14.
 */
@RestController
@RequestMapping("/menu")
public class MenuController {
    Logger logger = LoggerFactory.getLogger(MenuController.class);

    @Autowired
    private MenuService menuService;


    @RequestMapping(value = "/getList", method = RequestMethod.POST, produces = "application/json; charset=UTF-8")
    public String getList() {
        return  JSON.toJSON( menuService.getList()).toString();
    }



    @RequestMapping(value = "/delete", method = RequestMethod.POST, produces = "application/json; charset=UTF-8")
    public String delete(@RequestBody MenuModel menuModel) {
        logger.debug(this.getClass().getName(),JSON.toJSON(menuModel));
        try{
            boolean flag =  menuService.delete(menuModel.getMenuCode());
            if(flag){
                return ResponseOptUtil.commonSucess();
            }else{
                return  ResponseOptUtil.commonMsgError("请先删除子菜单");
            }

        }catch (Exception e){
            e.printStackTrace();
            return  ResponseOptUtil.commonMsgError("删除菜单失败");
        }
    }

    @RequestMapping(value = "/add", method = RequestMethod.POST, produces = "application/json; charset=UTF-8")
    public String add(@RequestBody MenuModel menuModel) {
        try{
            boolean flag = menuService.add(menuModel);
            if(flag){
                return ResponseOptUtil.commonSucess();
            }else{
                return  ResponseOptUtil.commonMsgError("角色代码已存在");
            }
        }catch (Exception e){
            e.printStackTrace();
            return  ResponseOptUtil.commonMsgError("添加菜单失败");
        }
    }


    @RequestMapping(value = "/findMenuByCode", method = RequestMethod.POST, produces = "application/json; charset=UTF-8")
    public String findMenuByCode(@RequestBody MenuModel menuModel) {

        try{
            MenuModel menuModel1  =  menuService.findMenuByCode(menuModel.getMenuCode());
            if(menuModel1!=null){
                return JSON.toJSONString(menuModel1);
            }else{
                return  ResponseOptUtil.commonMsgError("菜单不存在");
            }
        }catch (Exception e){
            e.printStackTrace();
            return  ResponseOptUtil.commonMsgError("添加菜单失败");
        }
    }


    @RequestMapping(value = "/update", method = RequestMethod.POST, produces = "application/json; charset=UTF-8")
    public String update(@RequestBody MenuModel menuModel) {
        try{
            menuService.update(menuModel);
            return ResponseOptUtil.commonSucess();
        }catch (Exception e){
            e.printStackTrace();
            return  ResponseOptUtil.commonMsgError("修改菜单失败");

        }
    }


}
